package com.shujia.filter;

import com.shujia.utils.HbaseTool;
import org.apache.hadoop.hbase.CompareOperator;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.util.Iterator;

/**
 *  三大类过滤器：
 *      1. 比较过滤器
 *      2. 专用过滤器
 *      3. 包装过滤器
 *
 * 比较过滤器：
 *    1、通过RowFilter与BinaryComparator过滤比rowKey 1500100010小的所有值出来
 *    2、通过FamilyFilter与SubstringComparator查询列簇名包含in的所有列簇下面的数据
 */
public class HBaseBiJiaoFilter {
    public static void main(String[] args) throws Exception{
        //1、通过RowFilter与BinaryComparator过滤比rowKey 1500100010小的所有值出来
//        xuQiu1();
//        HbaseTool.createHbaseTable("students2","info","inter","hadoop","oppo");

        //2、通过FamilyFilter与SubstringComparator查询列簇名包含in的所有列簇下面的数据
//        xuQiu2();

        //3、通过QualifierFilter与SubstringComparator查询列名包含ge的列的值
//        xuQiu3();

        //4、通过ValueFilter与BinaryPrefixComparator过滤出所有的cell中值以 "张" 开头的学生
        //只会查询出列值是张前缀的这一列，其他列不查
//        xuQiu4();

        //5、组合过滤器
        //查询列名叫name且，列值以'张'开头的
        xuQiu5();

    }

    public static void xuQiu5() throws Exception{
        //获取表对象
        Table students2 = HbaseTool.getOneTable("students2");
        Scan scan = new Scan();

        //创建前缀比较器
        BinaryPrefixComparator binaryPrefixComparator = new BinaryPrefixComparator(Bytes.toBytes("张"));
        //创建列值过滤器
        //public ValueFilter(final CompareOperator valueCompareOp, final ByteArrayComparable valueComparator)
        ValueFilter valueFilter = new ValueFilter(CompareOperator.EQUAL, binaryPrefixComparator);

        BinaryComparator binaryComparator = new BinaryComparator(Bytes.toBytes("name"));
        QualifierFilter qualifierFilter = new QualifierFilter(CompareOperator.EQUAL, binaryComparator);

        //设置过滤器
        //Scan setFilter(Filter filter)
//        scan.setFilter(qualifierFilter);
//        scan.setFilter(valueFilter); // 会将前一个setFilter进行覆盖

        //创建一个FilterList对象
        FilterList filterList = new FilterList();
        filterList.addFilter(valueFilter);
        filterList.addFilter(qualifierFilter);
        scan.setFilter(filterList);

        ResultScanner studentsScanner = students2.getScanner(scan);
        for (Result rs : studentsScanner) {
            HbaseTool.printResult(rs);
        }

        HbaseTool.close();
    }

    public static void xuQiu4() throws Exception{
        //获取表对象
        Table students2 = HbaseTool.getOneTable("students2");
        Scan scan = new Scan();

        //创建前缀比较器
        BinaryPrefixComparator binaryPrefixComparator = new BinaryPrefixComparator(Bytes.toBytes("张"));

        //创建列值过滤器
        //public ValueFilter(final CompareOperator valueCompareOp, final ByteArrayComparable valueComparator)
        ValueFilter valueFilter = new ValueFilter(CompareOperator.EQUAL, binaryPrefixComparator);

        //设置过滤器
        //Scan setFilter(Filter filter)
        scan.setFilter(valueFilter);
        ResultScanner studentsScanner = students2.getScanner(scan);
        for (Result rs : studentsScanner) {
            HbaseTool.printResult(rs);
        }

        HbaseTool.close();
    }
//put 'students2','1500101000','info:address','张家界'

    public static void xuQiu3() throws Exception{
        //获取表对象
        Table students2 = HbaseTool.getOneTable("students2");
        Scan scan = new Scan();
        //创建一个包含比较器 SubstringComparator
        SubstringComparator substringComparator = new SubstringComparator("ge");


        //创建列名过滤器 QualifierFilter
        //public QualifierFilter(final CompareOperator op, final ByteArrayComparable qualifierComparator)
        QualifierFilter qualifierFilter = new QualifierFilter(CompareOperator.EQUAL, substringComparator);

        //设置过滤器
        //Scan setFilter(Filter filter)
        scan.setFilter(qualifierFilter);
        ResultScanner studentsScanner = students2.getScanner(scan);
        for (Result rs : studentsScanner) {
            HbaseTool.printResult(rs);
        }

        HbaseTool.close();
    }


    public static void xuQiu2() throws Exception{
        //获取表对象
        Table students2 = HbaseTool.getOneTable("students2");
        Scan scan = new Scan();
        //创建一个包含比较器 SubstringComparator
        SubstringComparator substringComparator = new SubstringComparator("in");

        //新版本
        //public FamilyFilter(final CompareOperator op, final ByteArrayComparable familyComparator)
        FamilyFilter familyFilter = new FamilyFilter(CompareOperator.EQUAL, substringComparator);

        //设置过滤器
        //Scan setFilter(Filter filter)
        scan.setFilter(familyFilter);
        ResultScanner studentsScanner = students2.getScanner(scan);
        for (Result rs : studentsScanner) {
            HbaseTool.printResult(rs);
        }

        HbaseTool.close();
    }

    public static void xuQiu1() throws Exception{
        //获取表对象
        Table students = HbaseTool.getOneTable("students");
        Scan scan = new Scan();
        //创建一个二进制比较器 BinaryComparator
        //public BinaryComparator(byte[] value)
//        BinaryComparator binaryComparator = new BinaryComparator(Bytes.toBytes("1500100010"));
        //创建一个包含比较器 SubstringComparator
        SubstringComparator substringComparator = new SubstringComparator("1003");


        //创建行键过滤器
        //老版本
        //RowFilter(final CompareOp rowCompareOp, final ByteArrayComparable rowComparator)
//        RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.LESS, binaryComparator);
        //新版本
        //public RowFilter(final CompareOperator op, final ByteArrayComparable rowComparator)
        RowFilter rowFilter = new RowFilter(CompareOperator.EQUAL, substringComparator);

        //设置过滤器
        //Scan setFilter(Filter filter)
        scan.setFilter(rowFilter);
        ResultScanner studentsScanner = students.getScanner(scan);
        for (Result rs : studentsScanner) {
            HbaseTool.printResult(rs);
        }

        HbaseTool.close();
    }
}
